package main

import (
	"bytes"
	"encoding/gob"
	"fmt"
	"log"
	"strconv"
	"time"
	"unsafe"
)

type Node struct {
	Id    string
	Num   int
	table map[string]string
}

func main1() {
	r := []byte{1, 23, 4, 5, 6}
	var ptr [5]*byte
	for i := 0; i < len(r); i++ {
		ptr[i] = &r[i]
		fmt.Println("r:", r[i], *ptr[i])
		fmt.Println("地址", &r[0], &r[i], ptr[i])
	}

	fmt.Println(r)
	fmt.Println(ptr)

	rr := []byte{}
	// for i := 0; i < 5; i++ {
	// 	&rr[i] = ptr[i]
	// }

	fmt.Println(rr)
	fmt.Println(&rr)

}

func main2() {
	node := new(Node)
	node.table = make(map[string]string)
	fmt.Println("数据", node)
	fmt.Println("地址：", &node)
}

func main3() {
	data := []string{"apple", "bananna", "change"}
	fmt.Println("datalen=", data, len(data), data[2])
	fmt.Println("地址：", &data, &data[0])

	// 强转
	data1 := "apple"
	bd := []byte(data1)
	fmt.Println("bd=", bd) //97 112 112 108 101

	// 使用unsafe 强转
	var bd2 *[]byte
	bd2 = new([]byte)
	for i := 0; i < len(data); i++ {
		bdt := (*[]byte)(unsafe.Pointer(&data[i]))
		for j := 0; j < len(*bdt); j++ {
			*bd2 = append(*bd2, (*bdt)[j])
		}

	}
	fmt.Println("bd2=", *bd2)
	fmt.Printf("格式化输出%s\n", *bd2)

	// sting[] 转string 再转byte
	var data2 string
	for i := 0; i < len(data); i++ {
		data2 += data[i]
	}
	bd3 := []byte(data2)
	fmt.Println("bd3=", bd3)    //97 112 112 108 101.
	fmt.Printf("bd3=%s\n", bd3) //97 112 112 108 101

	// 转byte
	var r bytes.Buffer
	encoder := gob.NewEncoder(&r)
	encoder.Encode(data)

	fmt.Println("encode:", r.Bytes()) //97 112 112 108 101

	var e []string
	decoder := gob.NewDecoder(bytes.NewReader(r.Bytes()))
	decoder.Decode(&e)

	fmt.Println("decode:", e)

}

// 12 255 129 2 1 2 255 130 0 1 12 0 0 25 255 130 0 3 5  这些是不可显示的字符

// 定义请求结构
type Data struct {
	Timestamp int64
	Msg       string
}

func main() {
	t1 := time.Now().UnixNano()
	msg1 := "hello"

	time.Sleep(3 * time.Second)
	t2 := time.Now().Unix()
	msg2 := "world"
	data := []Data{{t1, msg1}, {t2, msg2}}

	for i := 0; i < 10; i++ {
		d := new(Data)
		d.Msg = "test" + strconv.Itoa(i)
		d.Timestamp = time.Now().UnixNano()
		data = append(data, *d)
	}

	r := EncodeBlockData(data)
	fmt.Println("encode:", r)

	e := DecodeBlockData(r)
	ee := e.([]Data)
	elen := len(ee)
	fmt.Println("decode:", ee)
	fmt.Println("输出最后三项：", ee[elen-3], ee[elen-2], ee[elen-1])
	fmt.Println("decodelen:", elen)

	// fmt.Println("测试单条数据--------------")
	// bd := new(Data)
	// bd.Timestamp = time.Now().UnixNano()
	// bd.Msg = "1234456"

	// var bdd []Data
	// bdd = append(bdd, *bd)

	// r = EncodeBlockData(bdd)
	// fmt.Println("encode:", r)

	// e = DecodeBlockData(r)
	// fmt.Println("decode:", e)

}

func DecodeBlockData(data []byte) interface{} {
	var ret []Data
	decoder := gob.NewDecoder(bytes.NewReader(data))
	err := decoder.Decode(&ret)
	if err != nil {
		log.Fatal(err)
	}
	return ret
}

func EncodeBlockData(Bd interface{}) []byte {
	var r bytes.Buffer
	encoder := gob.NewEncoder(&r)
	err := encoder.Encode(Bd)
	if err != nil {
		log.Panic(err)
	}
	return r.Bytes()
}

func main5() {
	for i := 0; i < 4; i++ {
		fmt.Println(i, "%4 = ", i%4)
	}
	fmt.Println("强转:", strconv.Itoa(10))
}
